package com.bookdresser.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bookdresser.R;
import com.bookdresser.model.BookItem;

public class BooksAdapter extends BaseAdapter {
	private List<BookItem> books;
	private Context context;

	public BooksAdapter(Context context, List<BookItem> books) {
		this.books = books;
		this.context = context;
	}

	@Override
	public int getCount() {
		return books.size();
	}

	@Override
	public Object getItem(int position) {
		return books.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final BookItem item = (BookItem) getItem(position);
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_item, parent, false);
			holder = new ViewHolder();
			holder.ivImage = (ImageView) convertView
					.findViewById(R.id.item_iv_image);
			holder.tvTitle = (TextView) convertView
					.findViewById(R.id.item_tv_title);
			holder.tvDescription = (TextView) convertView
					.findViewById(R.id.item_tv_description);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.ivImage.setImageDrawable(item.getIcon());
		holder.tvTitle.setText(item.getName());
		holder.tvDescription.setText(item.getDescription());

		return convertView;
	}

	static class ViewHolder {
		ImageView ivImage;
		TextView tvTitle;
		TextView tvDescription;
	}

}
